/**
 * redux - store.subscribe(函数)
 * 作用：监听state的变化，改变时触发回调函数
 * 场景: 本地存储，设置标题等等
 *
 * 1. store中是同步执行的，
 *    什么时候开始监听，什么时候才生效
 *
 * 2. 会返回一个函数，用来停止监听
 *
 */
import { legacy_createStore as createStore } from 'redux';

const store = createStore((state = { count: 0 }, action) => {
  // console.log('action -----> ', action);
  if (action.type === 'add') {
    return { ...state, count: state.count + action.payload };
  }
  if (action.type === 'dec') {
    return { ...state, count: state.count - action.payload };
  }
  return state;
});

// console.log('store -----> ', store);
// console.log('store.getState() -----> ', store.getState().count);

const unFn = store.subscribe(() => {
  console.log('state状态改变了 -----> ', store.getState().count);
});

store.dispatch({ type: 'add', payload: 1 });
store.dispatch({ type: 'add', payload: 10 });
store.dispatch({ type: 'dec', payload: 1 });
// 停止监听，后面在改变，不会再触发回调
unFn();
store.dispatch({ type: 'dec', payload: 10 });
// store.dispatch({ type: 'dec' });
// console.log('store.getState() -----> ', store.getState().count);

export default function App(params) {
  return <></>;
}
